/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ProjectView.h"
#include "ui_ProjectView.h"
#include "Project.h"
#include "Expression3D.h"
#include "Surface3D.h"
#include "Curve3D.h"
#include "Parametric3DDialog.h"
#include "Expression3DDialog.h"

ProjectView::ProjectView(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::ProjectView)
{
    m_ui->setupUi(this);
    connect(m_ui->curveButton,SIGNAL(clicked()),this,SLOT(newCurve()));
    connect(m_ui->exprButton,SIGNAL(clicked()),this,SLOT(newExpression()));
    connect(m_ui->surfaceButton,SIGNAL(clicked()),this,SLOT(newSurface()));
    connect(m_ui->delButton,SIGNAL(clicked()),this,SLOT(deleteCurrentObject()));
    connect(m_ui->treeWidget,SIGNAL(itemSelectionChanged()),this,SLOT(onItemSelected()));
}

ProjectView::~ProjectView()
{
    delete m_ui;
}

void ProjectView::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void ProjectView::setProject(Project* project){
    _project = project;
    update();
    connect(_project,SIGNAL(changed()),this,SLOT(update()));
}

void ProjectView::update() {
    m_ui->treeWidget->clear();
    QTreeWidgetItem *item;
    if(_project){
        item = new QTreeWidgetItem(m_ui->treeWidget);
        item->setText(0,"Equations Sets");
        QList<Expression3D*>::iterator e;
        for(e=_project->_expressions.begin();e!=_project->_expressions.end();++e){
            QTreeWidgetItem * expr = new QTreeWidgetItem(item);
            expr->setText(0,(*e)->objectName());
        }
        item = new QTreeWidgetItem(m_ui->treeWidget);
        item->setText(0,"Plots");
        QList<ParametricObject3D*>::iterator p;
        for(p=_project->_plots.begin();p!=_project->_plots.end();++p){
            QTreeWidgetItem * plotItem = new QTreeWidgetItem(item);
            plotItem->setText(0,(*p)->objectName());
        }

    }
}


void ProjectView::newSurface(){
    if(_project){
        Surface3D *surf = new Surface3D(_project);
        Parametric3DDialog dlg(surf,this);
        int result = dlg.exec();

        switch(result){
            case QDialog::Accepted:
                _project->addSurface(surf);
                break;
            case QDialog::Rejected:
                delete surf;
                break;
            default:
                break;
        }
    }

}
void ProjectView::newCurve(){
    if(_project){
        Curve3D *curve = new Curve3D(_project);
        Parametric3DDialog dlg(curve,this);
        int result = dlg.exec();

        switch(result){
            case QDialog::Accepted:
                _project->addCurve(curve);
                break;
            case QDialog::Rejected:
                delete curve;
                break;
            default:
                break;
        }
    }

}
void ProjectView::newExpression(){
    if(_project){
        Expression3D *expr = new Expression3D(_project);
        Expression3DDialog dlg(expr,this);
        int result = dlg.exec();
        switch(result){
            case QDialog::Accepted:
                _project->addExpression(expr);
                break;
            case QDialog::Rejected:
                delete expr;
                break;
            default:
                break;
        }
    }
}
void ProjectView::deleteCurrentObject(){
    if(_project){
        QTreeWidgetItem* item = m_ui->treeWidget->currentItem();
        QTreeWidgetItem* parent = item->parent();
        if(parent){
            if(parent->text(0)=="Equations Sets"){
                emit unselect();
                _project->deleteExpression(item->text(0));
                update();
            }
            else if(parent->text(0)=="Plots"){
                emit unselect();
                _project->deletePlot(item->text(0));
                update();
            }
        }
    }
}
void ProjectView::onItemSelected(){
    QTreeWidgetItem* item = m_ui->treeWidget->currentItem();
        QTreeWidgetItem* parent = item->parent();
        if(parent){
            if(parent->text(0)=="Equations Sets"){
                emit expressionSelected(item->text(0));
            }
            else if(parent->text(0)=="Plots"){
                emit plotSelected(item->text(0));
            }
        }
}
